package com.finddeer.bookkeeping.database;

import android.database.Cursor;
import android.database.CursorWrapper;

import com.finddeer.bookkeeping.Bill;
import com.finddeer.bookkeeping.database.BillDbSchema.BillTable;

import java.util.Date;
import java.util.UUID;

/**
 * author    : leaves
 * gitee     : https://gitee.com/LEAVES2018
 * time      : 2018/12/27 11:19 PM
 * function  : Cursor可以封装数据表中的原始字段值，封装Cursor对象，并添加有用的扩展方法（如getBill获取账单方法）。
 */

public class BillCursorWrapper extends CursorWrapper {
    public BillCursorWrapper(Cursor cursor) {
        super(cursor);
    }

    public Bill getBill() {
        String uuidString = getString(getColumnIndex(BillTable.Cols.UUID));
        String title = getString(getColumnIndex(BillTable.Cols.TITLE));
        double amount = getDouble(getColumnIndex(BillTable.Cols.AMOUNT));
        String tag = getString(getColumnIndex(BillTable.Cols.TAG));
        long date = getLong(getColumnIndex(BillTable.Cols.DATE));
        String locale = getString(getColumnIndex(BillTable.Cols.LOCALE));

        Bill bill = new Bill(UUID.fromString(uuidString));
        bill.setTitle(title);
        bill.setAmount(amount);
        bill.setTag(tag);
        bill.setDate(new Date(date));
        bill.setLocale(locale);

        return bill;
    }
}